import pyperclip
from pynput.keyboard import Key, Listener

flag = False
list = []
copy = True
index =0
def on_press(key):
    global copy
    global list
    global  index
    try:
        if  key == Key.ctrl_l:
            if (not copy) & (index < len(list)):
                pyperclip.copy(list[index])

        if not key == Key.ctrl_l:
            if (not copy) & (index < len(list)):

                index =index+1



    except AttributeError:
        print()


def on_release(key):

    global  copy
    global  list
    global  index
    try:
        if key ==Key.ctrl_l:
            pass
        elif key == Key.enter:
            pass
        elif key ==Key.left:
            list = []
            copy = True
        elif key == Key.right:
            copy = False
            index = 0
        elif key == Key.esc:
            return False

        else:
            if copy:

                list.append(pyperclip.paste())
                print(list)


    except AttributeError:
        print()


# Collect events until released
def func():
    with Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()
while True:
    input("输入 a 继续\n")
    func()

